//
//  MakeAnAppointmentToDoLaundryViewController.m
//  BaseFrame
//
//  Created by 吕金状 on 2017/8/8.
//  Copyright © 2017年 com.mxh. All rights reserved.
//

#import "MakeAnAppointmentToDoLaundryViewController.h"
#import "MakeAnAppointmentLeftTableViewCell.h"
#import "MakeAnAppointmentRightTableViewCell.h"
#import "RootMakeSureTheOrderViewController.h"

@interface MakeAnAppointmentToDoLaundryViewController ()<UITableViewDelegate , UITableViewDataSource>
{
    NSString *chooseLeftIndex;
}
Strong UIView *headView;
Strong UITableView *leftTableView;
Strong UITableView *rightTableView;
Strong NSMutableArray *leftDataArray; /**<*/
Strong NSMutableArray *rightDataArray; /**<*/
Strong UIView *footView;
Strong UILabel *moneyLabel;

@end

//网络
@implementation MakeAnAppointmentToDoLaundryViewController (NetWorking)

- (void)getData:(NSString *)category{
    [self showprogressHUD];
    NSDictionary *parameters = @{@"category":category,
                                 @"aId":self.aId};
    [[AFAppDotNetAPIClient sharedClient] POST:@"wardrobe/Mobile_Phone_Terminal/sortView.do" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenProgressHUD];
        if ([responseObject[@"responseStatus"] isEqualToString:@"0"]) {
            int a =  [chooseLeftIndex intValue];
            NSMutableArray *array1 = @[].mutableCopy;
            NSArray *array = responseObject[@"obj"];
            for (NSDictionary *dic in array) {
                NSMutableDictionary *dic1 = [NSMutableDictionary dictionaryWithDictionary:dic];
                dic1[@"number1"] = @"0";
                [array1 addObject:dic1];
            }
            self.rightDataArray[a] = array1;
            [self.rightTableView reloadData];
        }else{
            ShowMessage(responseObject[@"msg"]);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self hiddenProgressHUD];
        [self showError:error];
    }];
}

@end


//ui
@implementation MakeAnAppointmentToDoLaundryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"预约洗衣";
    chooseLeftIndex = @"0";
    [self.view sd_addSubviews:@[self.leftTableView , self.rightTableView , self.headView , self.footView]];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - click

- (void)setAId:(NSString *)aId{
    _aId = aId;
    [self getData:@"1"];
}

#pragma mark - init

- (UILabel *)moneyLabel{
    if (!_moneyLabel) {
        _moneyLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 1, SCREEN_WIDTH - 86 / WIDTH_5S_SCALE, 50 / WIDTH_5S_SCALE - 1)];
        _moneyLabel.backgroundColor = getColor(@"ffffff");
        _moneyLabel.textColor = getColor(mainColor);
        _moneyLabel.textAlignment = NSTextAlignmentCenter;
        _moneyLabel.attributedText = [self getAttributeName:@"0.00" info:@"0"];
    }
    return _moneyLabel;
}

- (NSAttributedString *)getAttributeName:(NSString *)name info:(NSString *)info{
    NSMutableAttributedString *attStr = [NSMutableAttributedString new];
    NSAttributedString *nameAtt = [[NSAttributedString alloc]initWithString:@"总计:" attributes:@{NSFontAttributeName:DEF_FontSize_17}];
    NSAttributedString *infoAtt = [[NSAttributedString alloc]initWithString:[NSString stringWithFormat:@"(数量:%@)",info] attributes:@{NSFontAttributeName:DEF_FontSize_12}];
    [attStr appendAttributedString:nameAtt];
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:name]];
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:@"元"]];
    [attStr appendAttributedString:infoAtt];
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setParagraphSpacing:16];//调整行间距
    [paragraphStyle setAlignment:NSTextAlignmentCenter];
    [attStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [attStr.mutableString length])];
    
    return attStr;
}

- (UIView *)footView{
    if (!_footView) {
        _footView = [[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 50 / WIDTH_5S_SCALE - 64, SCREEN_WIDTH, 50 / WIDTH_5S_SCALE)];
        _footView.backgroundColor = getColor(mainColor);
        UIButton *typeButton = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH - 86 / WIDTH_5S_SCALE, 0, 86 / WIDTH_5S_SCALE, 50 / WIDTH_5S_SCALE)];
        typeButton.backgroundColor = getColor(mainColor);
        typeButton.titleLabel.font = DEF_FontSize_14;
        [typeButton setTitle:@"结算" forState:UIControlStateNormal];
        [typeButton setTitleColor:getColor(@"ffffff") forState:UIControlStateNormal];
        [typeButton addTarget:self action:@selector(typeButtonClick) forControlEvents:UIControlEventTouchUpInside];
        
        [_footView sd_addSubviews:@[typeButton , self.moneyLabel]];
    }
    return _footView;
}

- (UIView *)headView{
    if (!_headView) {
        _headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 180 / WIDTH_5S_SCALE)];
       UIImageView *titleImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200 / WIDTH_5S_SCALE)];
        titleImageView.contentMode = UIViewContentModeScaleAspectFill;
        titleImageView.image = [UIImage imageNamed:@"xiyi"];
        [_headView addSubview:titleImageView];
        _headView.backgroundColor = getColor(mainColor);
    }
    return _headView;
}

- (UITableView *)leftTableView{
    if (!_leftTableView) {
        _leftTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 200 / WIDTH_5S_SCALE, 94 / WIDTH_5S_SCALE, SCREEN_HEIGHT - 64 - 200 / WIDTH_5S_SCALE - 50 / WIDTH_5S_SCALE) style:UITableViewStyleGrouped];
        _leftTableView.delegate = self;
        _leftTableView.dataSource = self;
        _leftTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _leftTableView.sectionFooterHeight = .01;
        _leftTableView.sectionHeaderHeight = .01;
        _leftTableView.tableFooterView = [UIView new];
        _leftTableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0.01)];
        _leftTableView.showsVerticalScrollIndicator = NO;
        _leftTableView.showsHorizontalScrollIndicator = NO;
        _leftTableView.backgroundColor = getColor(@"ffffff");
    }
    return _leftTableView;
}

- (UITableView *)rightTableView{
    if (!_rightTableView) {
        _rightTableView = [[UITableView alloc]initWithFrame:CGRectMake(94 / WIDTH_5S_SCALE, 200 / WIDTH_5S_SCALE, SCREEN_WIDTH - 94 / WIDTH_5S_SCALE, SCREEN_HEIGHT - 64 - 200 / WIDTH_5S_SCALE - 50 / WIDTH_5S_SCALE) style:UITableViewStyleGrouped];
        _rightTableView.delegate = self;
        _rightTableView.dataSource = self;
        _rightTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _rightTableView.sectionFooterHeight = .01;
        _rightTableView.sectionHeaderHeight = .01;
        _rightTableView.tableFooterView = [UIView new];
        _rightTableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0.01)];
        _rightTableView.showsVerticalScrollIndicator = NO;
        _rightTableView.showsHorizontalScrollIndicator = NO;
        _rightTableView.backgroundColor = getColor(@"ffffff");
    }
    return _rightTableView;
}

#pragma mark - click

//结算
- (void)typeButtonClick{
    RootMakeSureTheOrderViewController *vc = [RootMakeSureTheOrderViewController new];
    NSMutableArray *nary = [NSMutableArray array];
    for (int i = 0 ; i < self.rightDataArray.count; i ++) {
        for (int y = 0; y < [self.rightDataArray[i] count]; y ++) {
            if (![self.rightDataArray[i][y][@"number1"] isEqualToString:@"0"]) {
                [nary addObject:self.rightDataArray[i]];
            }
        }
    }
    //判断是否有数据
    if (nary.count > 0) {
        vc.mutableDataArray = nary;
        vc.eId = self.eId;
        [self.navigationController pushViewController:vc animated:YES];
    }else{
        ShowMessage(@"没有要结算的数据");
    }
}

#pragma mark - <UITableViewDelegate , UITableViewDataSource>

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView == self.leftTableView) {
        return self.leftDataArray.count;
    }
    if (self.rightDataArray.count == 0) {
        return 0;
    }
    return [self.rightDataArray[[chooseLeftIndex intValue]] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == self.leftTableView) {
        MakeAnAppointmentLeftTableViewCell *cell = [MakeAnAppointmentLeftTableViewCell cellForTableView:tableView];
        cell.indexPath = indexPath;
        cell.chooseIndex = chooseLeftIndex;
        cell.model = self.leftDataArray[indexPath.row];
        //cell.accessoryType = indexPath.row == 3 ?  UITableViewCellAccessoryDisclosureIndicator : UITableViewCellAccessoryNone;
        return cell;
    }
    MakeAnAppointmentRightTableViewCell *cell = [MakeAnAppointmentRightTableViewCell cellForTableView:tableView];
    cell.model = self.rightDataArray[[chooseLeftIndex intValue]][indexPath.row];
    cell.getAddNumberClick = ^{
        int a = [self.rightDataArray[[chooseLeftIndex intValue]][indexPath.row][@"number1"] intValue];
        a ++;
        self.rightDataArray[[chooseLeftIndex intValue]][indexPath.row][@"number1"] = [NSString stringWithFormat:@"%d",a];
        [self.rightTableView reloadData];
        int number = 0;
        int money = 0;
        for (int i = 0 ; i < self.rightDataArray.count; i ++) {
            for (int y = 0; y < [self.rightDataArray[i] count]; y ++) {
                if (![self.rightDataArray[i][y][@"number1"] isEqualToString:@"0"]) {
                    number += [self.rightDataArray[i][y][@"number1"] intValue];
                    money += [self.rightDataArray[i][y][@"price"] intValue] * [self.rightDataArray[i][y][@"number1"] intValue];
                }
            }
        }
        self.moneyLabel.attributedText = [self getAttributeName:[NSString stringWithFormat:@"%.2f",(float)money] info:[NSString stringWithFormat:@"%d",number]];
    };
    cell.getDeleteNumberClick = ^{
        int a = [self.rightDataArray[[chooseLeftIndex intValue]][indexPath.row][@"number1"] intValue];
        a --;
        self.rightDataArray[[chooseLeftIndex intValue]][indexPath.row][@"number1"] = [NSString stringWithFormat:@"%d",a];
        [self.rightTableView reloadData];
        int number = 0;
        int money = 0;
        for (int i = 0 ; i < self.rightDataArray.count; i ++) {
            for (int y = 0; y < [self.rightDataArray[i] count]; y ++) {
                if (![self.rightDataArray[i][y][@"number1"] isEqualToString:@"0"]) {
                    number += [self.rightDataArray[i][y][@"number1"] intValue];
                    money += [self.rightDataArray[i][y][@"price"] intValue] * [self.rightDataArray[i][y][@"number1"] intValue];
                }
            }
        }
        self.moneyLabel.attributedText = [self getAttributeName:[NSString stringWithFormat:@"%.2f",(float)money] info:[NSString stringWithFormat:@"%d",number]];
    };
    //cell.accessoryType = indexPath.row == 3 ?  UITableViewCellAccessoryDisclosureIndicator : UITableViewCellAccessoryNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == self.leftTableView) {
        chooseLeftIndex = [NSString stringWithFormat:@"%ld",(long)indexPath.row];
        if ([self.rightDataArray[[chooseLeftIndex intValue]] count] == 0) {
            [self getData:[NSString stringWithFormat:@"%d",[chooseLeftIndex intValue] + 1]];
        }
        [self.leftTableView reloadData];
        [self.rightTableView reloadData];
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 58 / WIDTH_5S_SCALE;
}


#pragma mark - data

- (NSMutableArray *)leftDataArray{
    if (!_leftDataArray) {
        _leftDataArray  = @[@{@"image":@"xiyi1",@"title":@"上衣"},
                            @{@"image":@"xiyi2",@"title":@"大衣外套"},
                            @{@"image":@"xiyi3",@"title":@"裤装裙装"},
                            @{@"image":@"xiyi4",@"title":@"鞋子"},
                            @{@"image":@"xiyi5",@"title":@"靴子"},
                            @{@"image":@"xiyi6",@"title":@"家纺"},
                            @{@"image":@"xiyi7",@"title":@"配件"},
                        ].mutableCopy;
    }
    return _leftDataArray;
}

- (NSMutableArray *)rightDataArray{
    if (!_rightDataArray) {
        _rightDataArray  = @[
                             @[].mutableCopy,
                             @[].mutableCopy,
                             @[].mutableCopy,
                             @[].mutableCopy,
                             @[].mutableCopy,
                             @[].mutableCopy,
                             @[].mutableCopy,
                            ].mutableCopy;
    }
    return _rightDataArray;
}

@end
